package com.smt.modules.bus.service.impl;

import com.smt.common.enums.Code;
import com.smt.common.exception.RRException;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.R;
import com.smt.common.validator.Assert;
import com.smt.modules.bus.vo.MemberUserVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.lang.reflect.Member;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.Query;

import com.smt.modules.bus.dao.MemberUserDao;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.service.MemberUserService;


@Service("memberUserService")
public class MemberUserServiceImpl extends ServiceImpl<MemberUserDao, MemberUserEntity> implements MemberUserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<MemberUserEntity> page = this.selectPage(
                new Query<MemberUserEntity>(params).getPage(),
                new EntityWrapper<MemberUserEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public MemberUserEntity queryByMobile(String mobile) {
        return selectOne(new EntityWrapper<MemberUserEntity>()
                .eq("mobile", mobile));
    }

    @Override
    public MemberUserEntity queryByEmail(String email) {
        return selectOne(new EntityWrapper<MemberUserEntity>()
                .eq("email", email));
    }

    /**
     * 用户登录 手机号？邮箱
     *
     * @param form    登录表单
     * @return
     */
    @Override
    public long login(MemberUserVo form) {
        MemberUserEntity userEmail = null;
        MemberUserEntity userMobile = null;
        if(form.getMobile() == null){
            userEmail = queryByEmail(form.getEmail());
            if(Objects.isNull(userEmail)){
                return new Long(-1);
            }
            if(userEmail != null && !userEmail.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
                return new Long(0);
            }
            return userEmail.getUserId();
        }else{
            userMobile = queryByMobile(form.getMobile());
            if(Objects.isNull(userMobile)){
                return new Long(-1);
            }
            //密码错误
            if(!userMobile.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
                return new Long(0);
            }
            return userMobile.getUserId();
        }
    }

    /**
     * 更新用户文件总容量大小
     *
     * @param userId
     * @param fileSize
     * @return
     */
    @Override
    public boolean updateSumFileSize(Long userId, double fileSize) {
        MemberUserEntity memberUserEntity = new MemberUserEntity();
        memberUserEntity.setUpdateTime(DateTime.now().toDate());
        memberUserEntity.setUserId(userId);
        memberUserEntity.setSumSize(fileSize);
        return updateById(memberUserEntity);
    }

    /**
     * 用户信息编辑表 TODO
     *
     *
     * @param memberUserVo
     * @param memberUserEntity
     * @return
     */
    @Override
    public R edit(MemberUserVo memberUserVo, MemberUserEntity memberUserEntity) {
        Long userId = memberUserEntity.getUserId();
        memberUserVo.setUserId(userId);
        if(userId != null){
            //限制修改密码
            if(memberUserVo.getPassword() != null){
                return R.error(EnumToKeyUtil.toInt(Code.MODIFY_DATA_ERROR),"禁止修改密码");
            }
            MemberUserEntity memberUser = memberUserVo.copyPropertiesTo(memberUserEntity);
            memberUser.setUserId(userId);
            Integer count = baseMapper.updateById(memberUser);
            return count == 0 ? R.error(EnumToKeyUtil.toInt(Code.APPEND_DATA_ERROR),"用户更新失败"):R.ok();
         }else {
            return R.error(EnumToKeyUtil.toInt(Code.MODIFY_DATA_ERROR),"非创建用户");
        }
    }



}
